<?php
defined('_JEXEC') or die('Restricted access');

class LogicHelper
{
    public static function generateCouponCodes($lenght)
    {   
        $chars = 'abdefhiknrstyzABDEFGHKNQRSTYZ0123456789'; 
        $count_chars = strlen ($chars);  
        $string = "";
        
        for ($i=0; $i < $lenght; $i++) 
        {
            $rand = mt_rand (0,$count_chars-1); 
            $string .= substr ($chars, $rand, 1); 
        }  
        
        return $string;
    }
    
    public static function isDouble($var)
    {
        return preg_match("|^[\d]+\.?[\d]*$|", $var) == 1;
    }
    
    public static function isInt($var)
    {
        return preg_match("|^[\d]+$|", $var) == 1;
    }
    
    public static function isDateTime($var)
    {
        return preg_match("|^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$|", $var) == 1;
    }
    
    public static function getDateFromString($str)
    {
        $date = DateTime::createFromFormat('Y-m-d H:i:s', $str);
        return $date;
    }
    
    public static function deleteRecordsList($tableName)
    {
        $ids = JRequest::getVar('cid');
        $row = JTable::getInstance($tableName, 'Table');
        foreach($ids as $id)
            if(!$row->delete($id))
            {
                $this->setError("Error deleting id=".$id);
                return false;
            }
        return true;
    }
}
?>
